"use strict";
/* Controllers */
app.controller("costTaskController", ['$scope', '$location', '$rootScope', '$routeParams', 'costTaskService', function ($scope, $location, $rootScope, $routeParams, costTaskService) {

    var vm = $scope.vm;
    vm.result = {};
    vm.p = {};
    vm.p.ownerId = $routeParams.ownerId;
    vm.p.dt = $routeParams.dt;
    vm.p.taskId = $routeParams.taskId;
    vm.p.type = 'task';


    if(typeof(vm.p.ownerId)=='undefined' || vm.p.ownerId=='0') {
        vm.p.ownerId = null;
    }
    if(typeof(vm.p.dt)=='undefined' || vm.p.dt=='0') {
        vm.p.dt = getNowFormatDate(-1);
    }

    var errorFn = function (result) {
        alert(result);
    };



    $scope.listCostTask = function () {
        var vm = $scope.vm;
        var params = {};
        params = vm.p;
        $('#list_table').bootstrapTable('showLoading');
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;

            $('#list_table').bootstrapTable('load', data);
            $('#list_table').bootstrapTable('hideLoading');
        };
        vm.result.ms = '...';
        costTaskService.listCostTask(params, procFn, errorFn);
    };

    $scope.clearSearch = function () {
        var vm = $scope.vm;
        vm.p.searchType = '';
        vm.p.endpoint = '';
        vm.p.areaName = '';
    };


    $scope.searchCostTask = function () {
        $('#list_table').bootstrapTable('refresh');
        $scope.listCostTask()
    };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 100, 1000], pageSize: 30
        });

        $resultTable.bootstrapTable('hideColumn', 'queueNames');

        $scope.listCostTask();
    };

    $scope.initializeController();
}]);


/* Services */
app.service('costTaskService', ['ajaxService', function (ajaxService) {

    this.listCostTask = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/cost_report/listCostReport4Owner.do", successFunction, errorFunction);
    };

}]);


function costTaskIdFmt(value, row, index) {
    return [
        '<a href="#/star/cost/cost_task_detail/', row.userName, '/', row.period, '/', row.dt, '" target="_blank">', " <u>查看明细</u>", '</a>'
    ].join('');
}


function minsCoresFmt(value, row, index) {
    value = (value*60);
    var flag = false;
    if(value<0) {
        flag = true;
        value = -value;
    }
    var rs = internationalNumberShortFormatterCpu(value,2, true);
    return (flag?'-':'')+ rs;
}


function userNameFmt(value, row, index) {
        if(value==null) return '-';
        return row.userNameCn + '('+value+')';
}


// 成本（阿里云）：150元/CU/月
function taskCostFmt(value, row, index) {
    if(value==null) return '-';
    var total = ((value/60/24/30)*150).toFixed(2);
    return internationalNumberFormatter(total);
}


function rateFormat(value, row, index) {
    if(value==null) return '-';
    return (value*100).toFixed(2) + '%';
}


/**
 * 格式化cpu
 * @param value
 * @param fixLength
 * @param isOriginUnit value是否是CU单位，true表示不是CU是Cs(second) * 100，false或者不写则为CU
 * @param isEnlarged 是否放大了100倍，true为放大了
 * @returns
 */
function internationalNumberShortFormatterCpu(value, fixLength, isOriginUnit, isEnlarged) {
    if (value == null || typeof value == 'undefined' || isNaN(value)) {
        return "0";
    }
    if (!fixLength || isNaN(fixLength) || fixLength > 3 || fixLength < 0) {
        fixLength = 2;
    }

    if (isOriginUnit) {
        if (isEnlarged) {
            value = value / 100;
        }
        if (value < 60) {//秒级
            return value + "Cs";
        } else if (value < 60 * 60 * 24) {//分钟级
            return (value / 60).toFixed(fixLength) + "Cm";
        } else {
            value = (value / (60 * 60 * 24)).toFixed(fixLength);//天级
        }
    }

    value += '';
    var x = value.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    var times = 0;
    while (rgx.test(x1)) {
        times++;
        x2 = x1.replace(rgx, '$2');
        x1 = x1.replace(rgx, '$1');
    }
    if(x2<0) {
        x2 = -x2 + '';
    }
    if (x2.length > fixLength) {
        x2 = x2.substring(0, fixLength)
    }
    switch (times) {
        case 0:
            return internationalNumberFormatter(value) + "CU";
        case 1:
            return x1 + "." + x2 + "KCU";
        case 2:
            return x1 + "." + x2 + "MCU";
        case 3:
            return x1 + "." + x2 + "GCU";
        case 4:
            return x1 + "." + x2 + "TCU";
        case 5:
            return x1 + "." + x2 + "PCU";
        case 6:
            return x1 + "." + x2 + "ECU";
        case 7:
            return x1 + "." + x2 + "ZCU";
        case 8:
            return x1 + "." + x2 + "YCU";
        default:
            return internationalNumberFormatter(value);
    }
}

/**
 * 数据的国际化格式化方式，三个数据用逗号分隔 如123456转换成123,456
 */
function internationalNumberFormatter(value) {
    value += '';
    var x = value.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}